// Author: Bastian <bs@chemanet.de>

var ie  = document.all
var ns6 = document.getElementById && !document.all


document.write('<div id="popup_curtain" style="display: none; position: fixed; top: 0; left: 0; z-index: 1; background-color: black"><\/div>');


var POPUP_CURTAIN = false;
var POPUP_WINDOWS = new Array();


function popup_close ( id ){
	popup = document.getElementById( id );
	popup.style.display = "none";
	popup.style.width = "0px";
	popup.style.height = "0px";

	// remove popup in array
	tmp = new Array();
	n = 0;
	for( i=0; i < POPUP_WINDOWS.length; i++ ){
		if( POPUP_WINDOWS[ i ] != id ){
			tmp[ n ] = POPUP_WINDOWS[ i ];
			n += 1;
		}
	}
	POPUP_WINDOWS = tmp;

	// hide curtain if all popups has closed
	if( POPUP_WINDOWS.length < 1 ){
		curtain = document.getElementById("popup_curtain");
		curtain.style.width = "0px";
		curtain.style.height = "0px";
		curtain.style.display = "none";
		POPUP_CURTAIN = false;
	}
}

function popup_drop_curtain () {
	if( !POPUP_CURTAIN ){
		curtain = document.getElementById("popup_curtain");
		curtain.style.width = window.innerWidth + "px";
		curtain.style.height = window.innerHeight + "px";
		transparency(curtain, 30);
		curtain.style.display = "block";
		POPUP_CURTAIN = true;
	}
}

function popup_init ( name, src, width, height, top, left ) {
	for( i=0; i < POPUP_WINDOWS.length; i++ ){
		if( POPUP_WINDOWS[ i ] == "popup__" + name ){
			popup_close("popup__" + name);
		}
	}

	popup_drop_curtain();

	popup_iframe = document.createElement("iframe");
	attribute_style = document.createAttribute("style");
	attribute_style.nodeValue = "width: " + width + "px; height: " + height + "px;";
	popup_iframe.setAttributeNode(attribute_style);
	attribute_src = document.createAttribute("src");
	attribute_src.nodeValue = src;
	popup_iframe.setAttributeNode(attribute_src);

	popup_linktext = document.createElement("span");
	text = document.createTextNode( "close" );
	popup_linktext.appendChild(text);

	popup_closelink = document.createElement("a");
	popup_closelink.appendChild( popup_linktext );
	attribute_title = document.createAttribute("title");
	attribute_title.nodeValue = "Fenster schließen.";
	popup_closelink.setAttributeNode(attribute_title);
	attribute_href = document.createAttribute("href");
	attribute_href.nodeValue = "javascript:popup_close('popup__" + name + "');";
	popup_closelink.setAttributeNode(attribute_href);

	popup_title = document.createElement("h2");
	text = document.createTextNode( name );
	popup_title.appendChild(text);

	popup_bar = document.createElement("div");
	popup_bar.appendChild( popup_title );
	popup_bar.appendChild( popup_closelink );
	attribute_style = document.createAttribute("style");
	attribute_style.nodeValue = "height: 20px;";
	popup_bar.setAttributeNode(attribute_style);

	popup_div = document.createElement("div");
	popup_div.appendChild( popup_bar );
	popup_div.appendChild( popup_iframe );
	attribute_style = document.createAttribute("style");
	attribute_style.nodeValue = "display: none; position: fixed; z-index: 10;";
	popup_div.setAttributeNode(attribute_style);
	attribute_class = document.createAttribute("class");
	attribute_class.nodeValue = "popup_window";
	popup_div.setAttributeNode(attribute_class);
	attribute_id = document.createAttribute("id");
	attribute_id.nodeValue = "popup__" + name;
	popup_div.setAttributeNode(attribute_id);

	document.getElementsByTagName("body")[0].appendChild(popup_div);
	popup = document.getElementById( "popup__" + name );
	popup_div.style.width = width + "px";
	popup_div.style.height = (height + 20) + "px";



	// browserweiche
	if (ns6){
		if( top ) popup_div.style.top = top + "px";
		else popup_div.style.top = Math.ceil( (window.innerHeight - (height + 20)) / 2 ) + "px";

		if( left ) popup_div.style.left = left + "px";
		else popup_div.style.left = Math.ceil( (window.innerWidth - width) / 2 ) + "px";
	}
	else{
		// menu position
  		posX = window.event.clientX + document.documentElement.scrollLeft;
  		posY = window.event.clientY + document.documentElement.scrollTop;

		windoxX = document.body.clientWidth;
		windoxY = document.body.clientHeight;

		if( posX + menuObj.offsetWidth > windoxX ) menuObj.style.pixelLeft = posX - menuObj.offsetWidth;
		else menuObj.style.pixelLeft = posX;

		if( posY + menuObj.offsetHeight > windoxY ) menuObj.style.pixelTop = posY - menuObj.offsetHeight;
		else menuObj.style.pixelTop = posY;

		if( top ) popup_div.style.top = top + "px";
		else popup_div.style.pixelTop = Math.ceil( (document.body.clientHeight - (height + 20)) / 2 ) + "px";

		if( left ) popup_div.style.left = left + "px";
		else popup_div.style.pixelLeft = Math.ceil( (document.body.clientWidth - width) / 2 ) + "px";

	}
	popup_div.style.display = "block";

	POPUP_WINDOWS[ POPUP_WINDOWS.length ] = "popup__" + name;
	return "popup__" + name;
}

function transparency ( element, percentage ) {
	// Lokale Variablen definieren
	var i, count, objStyle, filterValue, opacityValue;

 	// Browser unterstuetzt (W3C-)DHTML?
 	if(document.getElementById) {
	  	// Wurde Object uebergeben und existiert es?
	  	if(typeof(element)=="object" && element) { obj=element; }
	  	// Ansonsten: Existiert (mindestens) ein HTML-Element mit passendem NAME-Attribut?
	  	else if (document.getElementsByName(element) && document.getElementsByName(element)[0]) { obj=document.getElementsByName(element); }
	  	// Ansonsten: Existiert ein HTML-Element mit passendem ID-Attribut?
	  	else if (document.getElementById(element)) { obj=document.getElementById(element); }
	  	// Ansonsten: Existiert (mindestens) ein passendes HTML-Element?
	  	else if (document.getElementsByTagName && document.getElementsByTagName(element) && document.getElementsByTagName(element)[0]) { obj=document.getElementsByTagName(element); }
	  	// Ansonsten: Kein passendes Objekt gefunden
	  	else { obj=false; }

	  	// Wenn ein Objekt existiert
	  	if(obj) {
		   	// Gueltigen Prozentwert definieren (Deckungswert)
		   	percentage=(typeof(percentage)=="undefined")?50:100-percentage;
		   	// Wert fuer (IE-)Filter
		   	filterValue="Alpha(opacity="+percentage+")";
		   	// Wert fuer CSS opacity
		   	opacityValue=""+percentage/100;
		   	// Anzahl der passenden Elemente bestimmen
		   	count=(obj.length)?obj.length:1;
		   	// Diese Elemente durchgehen
		   	for(i=0;i<count;i++) {
			    // (Arbeits-)Stylesheet-Objekt definieren
			    objStyle=(obj.length)?obj[i].style:obj.style;
			    // "filter(Alpha)"-Style setzen (fuer IE)
			    objStyle.filter=filterValue;
			    // "-moz-opacity"-Style setzen (fuer Mozilla)
			    objStyle.MozOpacity=opacityValue;
			    // "-khtml-opacity"-Style setzen (fuer Konqueror/Safari)
			    objStyle.KhtmlOpacity=opacityValue;
			    // "opacity"-Style setzen (fuer CSS-3-Browser)
			    objStyle.opacity=opacityValue;
			}
		}

	}
}